package com.ciallo.stock.face.impl;

import com.ciallo.stock.constant.StockConstant;
import com.ciallo.stock.face.StockCacheFace;
import com.ciallo.stock.mapper.StockBusinessMapper;
import com.ciallo.stock.pojo.entity.StockBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 42088
 * @version 1.0
 * @description: TODO
 * @date 2025/3/14 15:44
 */
@Component
@CacheConfig(cacheNames = StockConstant.STOCK)
public class StockCacheFaceImpl implements StockCacheFace {

    @Autowired
    private StockBusinessMapper stockBusinessMapper;

    /**
     * 缓存股票编码，且编码数据提供所属大盘前缀
     * @return
     */
    @Cacheable(key = "#root.method.getName()")
    @Override
    public List<String> getAllStockCodeWithPredix() {
        //1.获取所有A股股票的编码
        List<String> allCodes = stockBusinessMapper.getStockIds();
        //2.添加股票前缀 sh sz
        List<String> prefixCodes = allCodes.stream().map(code -> {
            code = code.startsWith("6") ? "sh" + code : "sz" + code;
            return code;
        }).collect(Collectors.toList());
        return prefixCodes;
    }

    /**
     * 清除缓存
     * 如果指定具体key，表达式需要添加单引号
     * @param info
     */
    @CacheEvict(key = "'getAllStockCodeWithPredix'")
    @Override
    public void updateStockInfoById(StockBusiness info) {
        stockBusinessMapper.updateByPrimaryKeySelective(info);
    }
}
